
package com.easyfun.healthmagicbox.comm;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import android.content.Context;

import com.easyfun.healthmagicbox.base.BaseActivity;
import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.comm.json.PersonQuestionsListJsonWrapper;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.ServerResponseCode;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.pojo.PersonQuestion;
import com.easyfun.healthmagicbox.sync.CommCallback;
import com.j256.ormlite.dao.Dao;

public class PersonQuestionsDownloadTask extends BaseCommTask implements DatabaseAccess{

	private String mpersonalID;

	public PersonQuestionsDownloadTask(Context context, String personID,
			CommCallback callback) {

		super(context,callback);
		mpersonalID = personID;
	}

	@Override
	public void run() {
		try {

			Dao<PersonQuestion, Integer> inputdao = getHelper(mcontext)
					.getQuestionsDao();

			long max = inputdao
				.queryRawValue(
							"select max(syncid) from PersonQuestion where personID = ?",
							mpersonalID);

			if (max > -1) {

				JSONObject serializedObj = new JSONObject();
				serializedObj.put("personID", mpersonalID);
				serializedObj.put("syncid", max);				

				Map<String, String> parmas = new HashMap<String, String>();
				parmas.put("jsonstring", serializedObj.toString());

				String result = super.uploadByHTTP(parmas,
						ConstantData.PersonQuestionDownloadURI);

				if (ServerResponseCode.parseOf(result)==ServerResponseCode.FAILED) {
					ccb.setCommResult(this,
							mpersonalID, ServerResponseCode.FAILED);
					return;
				}
				if (ServerResponseCode.parseOf(result)==ServerResponseCode.NOMORE) {
					ccb.setCommResult(this,
							mpersonalID, ServerResponseCode.NOMORE);
					return;
				}

				if (result.length()>0) {
					PersonQuestionsListJsonWrapper<?> wapper = PersonQuestionsListJsonWrapper
							.getInstance(result,PersonQuestionsListJsonWrapper.class);

					List<PersonQuestion> inputlist = wapper.getMpps();
					for (PersonQuestion input:inputlist)
					{
						List<PersonQuestion> data = inputdao.queryForEq("syncid", input.getSyncid());
						if(data.size() == 0){ //new one
							inputdao.create(input);
						}
					}
					ccb.setCommResult(this,
							mpersonalID, ServerResponseCode.SUCCESS);
				
				}else
				{
					ccb.setCommResult(this,
							mpersonalID, ServerResponseCode.FAILED);
				}

				return;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			if(e instanceof UnknownHostException) {
				ccb.setCommResult(this, mpersonalID, ServerResponseCode.NOCONNECTION);
			}else {
			ccb.setCommResult(this, mpersonalID,
					ServerResponseCode.FAILED);
			}
		} 

	}

	@Override
	public DatabaseHelper getHelper(Context context) {

		return DatabaseAccessImplementation.getInstance().getHelper(mcontext);
	}

}

